package drds.configuration.web.common.session_manager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

/**
 * 用户登录之后才能进行被管理,会话失效有两种:1会话主动注销2会话超时自动注销
 *
 * @author caozhenghui@drds.team<330937205@qq.com>
 */
public class SessionContextManager {
    public static final String ID = "_ID_";
    /**
     * session管理容器
     */
    public static ConcurrentHashMap<String, HttpSession> SessionContent_Map = new ConcurrentHashMap<String, HttpSession>();
    public static Logger logger = Logger.getLogger(SessionContextManager.class.getName());

    public static ConcurrentHashMap<String, HttpSession> getSessionContextMap() {
        return SessionContextManager.SessionContent_Map;
    }

    /**
     * 必须保证会话中含有挤下其他地方登录的凭证
     */
    public static void addSessionContent(HttpServletRequest request) {
        if (request == null) {
            throw new RuntimeException("request为空");
        }
        HttpSession session = request.getSession();

        _ID_ _ID_ = (_ID_) session.getAttribute(ID);
        if (_ID_ == null) {
            return;
        }

        Long id = _ID_.getId();
        if (id == null) {
            return;
        }
        String ids = id.longValue() + "";
        if (SessionContextManager.SessionContent_Map.containsKey(ids)) {
            HttpSession session_of_the_same = SessionContextManager.SessionContent_Map.remove(ids);
            try {
                session_of_the_same.invalidate();
            } catch (Exception e) {
            }
        }
        SessionContextManager.SessionContent_Map.put(ids, session);
    }

    /**
     * 直接从SessionContext-Map中删除一个SessionContext
     */
    public static void removeSessionContent(HttpServletRequest request) {
        if (request == null) {
            logger.info("request为空");
            return;
        }
        HttpSession session = request.getSession();
        _ID_ _ID_ = (_ID_) session.getAttribute(ID);
        if (_ID_ == null) {
            return;
        }

        Long id = _ID_.getId();
        if (id == null) {
            return;
        }
        String ids = id.longValue() + "";

        if (SessionContextManager.SessionContent_Map.containsKey(ids)) {
            SessionContextManager.SessionContent_Map.remove(ids);
        } else {
            logger.info("不能在sessionContextMap找到特定的sessionContext");
        }

    }

    public static HttpSession removeSessionContent(Long id) {
        if (id == null) {
            return null;
        }
        String ids = id.longValue() + "";
        if (SessionContextManager.SessionContent_Map.containsKey(ids)) {
            HttpSession session = SessionContent_Map.get(ids);
            SessionContextManager.SessionContent_Map.remove(ids);
            return session;
        } else {
            return null;
        }

    }

    public static HttpSession getSessionContent(Long id) {
        if (id == null) {
            return null;
        }
        String ids = id.longValue() + "";
        if (SessionContextManager.SessionContent_Map.containsKey(ids)) {
            HttpSession session = SessionContent_Map.get(ids);
            return session;
        } else {
            return null;
        }

    }

}
